// miniprogram/pages/user_list/user_list.js
const app = getApp();
const request = require('../../utils/request.js');
const config = require('../../utils/config');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    colorTheme: app.globalData.colorTheme,
    animationTime: 0.3,
    userList: [],
    pageNum: 1,
    pageSize: 50,
    params: {},

  },

  /**
   * 加载队友列表
   */
  listStudentByParam: function(pageNum,pageSize,params){
    let that = this;
    params = params ? params : {};
    let url = config.SERVER_URL + 'index/listStudentByParam?pageNum='+pageNum+'&pageSize='+pageSize;
    wx.showLoading({
      title: '加载ing',
    })
    request.requestGetApi(url,{},params,'application/json').then(res => {
      if(res.status === config.STATUS_OK){
        that.setData({
          userList: that.data.userList.concat(res.data.rows)
        })
      };
    }).catch(err => {
      app.failFun(err);
    }).finally((res) => {
      app.completeFun(res);
    });
    this.setData({
      params: params
    })
  },

  /**
 * 查看用户展示页
 */
  goUserShowPage: function (e) {
    let uid = e.currentTarget.dataset.uid;
    wx.navigateTo({
      url: '/pages/user-show-info/user-show-info?uid=' + uid,
    })
  },

  /**
   * 搜索
   */
  searchSubmit: function(e){
    let params = e.detail.value;
    this.setData({
      userList:[],
      pageNum:1
    });
    this.listStudentByParam(this.data.pageNum, this.data.pageSize, params);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.listStudentByParam(this.data.pageNum,this.data.pageSize);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      userList: []
    });
    this.listStudentByParam(this.data.pageNum,this.data.pageSize);

    wx.stopPullDownRefresh();
  },

  /**
   * 拉至底部
   */
  scrolltolower: function(){
    let pageNum = this.data.pageNum + 1;
    this.setData({
      pageNum: pageNum
    });
    this.listStudentByParam(pageNum,this.data.pageSize);
  },


})